class ApplicationController < ActionController::Base
  before_filter :authorize
  protect_from_forgery

  private
    def current_cart
      Carrito.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      cart = Carrito.create
      session[:cart_id] = cart.id
      cart
    end

  protected

    def authorize
      unless Cliente.find_by_id(session[:cliente_id])
        redirect_to acceder_url, notice: 'Por Favor inicie sesion'  
      end
    end
 
    def authorize_admin
      unless Cliente.find_by_id(session[:cliente_id]).is_admin
        redirect_to tienda_url, notice: 'No tiene los privilegios'  
      end
    end
 
  def ensure_logout
    if Cliente.find_by_id(session[:cliente_id])
      flash[:notice] = "Debes Registrarte para poder salir"
      redirect_to(tienda_url)
    end
  end

end  
